<?php
/**
 * Created by PhpStorm.
 * User: chang
 * Date: 16/8/21
 * Time: 下午5:18
 */

namespace Common\Util;


use GuzzleHttp\Client;

class CommonUtil
{
    public static function uuid($prefix = '')
    {
        $chars = md5(uniqid(mt_rand(), true));
        $uuid = substr($chars, 0, 8) . '-';
        $uuid .= substr($chars, 8, 4) . '-';
        $uuid .= substr($chars, 12, 4) . '-';
        $uuid .= substr($chars, 16, 4) . '-';
        $uuid .= substr($chars, 20, 12);
        return strtoupper($prefix . $uuid);
    }

    public static function isAjax() {
        if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) ) {
            if('xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH']))
                return true;
        }
        if(!empty($_POST[C('VAR_AJAX_SUBMIT')]) || !empty($_GET[C('VAR_AJAX_SUBMIT')]))
            return true;
        return false;
    }

    public static function postRequest($url,$param)
    {
        try {
            $response = (new Client())->post($url, array('query' => $param));

            $status = $response->getStatusCode();

            if ($status == "200") {
                $response = $response->getBody()->getContents();
            }
        } catch (\Exception $e) {
            $response = $e->getMessage();
        }
        return $response;
    }

    public static function getTimeString() {
        return date('Y-m-d H:i:s', time());
    }
}
